package com.att.control;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.RawImage;
import com.android.ddmlib.TimeoutException;
import com.sun.image.codec.jpeg.JPEGCodec;

public class ScreenCapture {

	private int screenPort;
	private ServerSocket ss;
	private boolean flag = true;
	
	public ScreenCapture(int port) {
		this.screenPort = port;
	}

	public void startService() {
		flag = true;
		Thread t = new Thread() {
			public void run() {
				System.out.println("screen capture service has been started");
				try {
					ss = new ServerSocket(screenPort);
					while (flag) {
						Socket socket = ss.accept();
						WorkerThread t = new WorkerThread(socket);
						
						t.start();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		};
		t.start();
	}
	
//	public void closeService(){
//		System.out.println("screen capture has been ended");
//		flag = false;
//		try {
//			if (ss != null) {
//				ss.close();
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		ss = null;
//	}

	class WorkerThread extends Thread {

		private Socket socket;

		public WorkerThread(Socket socket) {
			this.socket = socket;
		}

		private byte[] screenShot(IDevice device) throws IOException, TimeoutException, AdbCommandRejectedException {
			IDevice myPhone = device;
			RawImage raw = myPhone.getScreenshot();
			BufferedImage image = new BufferedImage(raw.width, raw.height,
					BufferedImage.TYPE_INT_RGB);
			int index = 0;
			int indexInc = raw.bpp >> 3;
			for (int y = 0; y < raw.height; y++) {
				for (int x = 0; x < raw.width; x++, index += indexInc) {
					int value = raw.getARGB(index);
					image.setRGB(x, y, value);
				}
			}
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			JPEGCodec.createJPEGEncoder(bos).encode(image);
			return bos.toByteArray();
		}

		@Override
		public void run() {
			System.out.println("capture worker has been connected");
//			IDevice device = ADBTool.device;
			IDevice device = AndroidProxy.getInstance().getDevice();
			try {
				InputStream in = socket.getInputStream();
				OutputStream out = socket.getOutputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				while (true) {
					String cmd = br.readLine();
					if (cmd != null && !"".equals(cmd)) {
						if ("screen".equals(cmd)) {
							byte[] b = screenShot(device);
							out.write(AttUtility.intToByteArray(b.length));
							out.write(b);
							out.flush();
							continue;
						}
						if ("quit".equals(cmd)) {
							break;
						}
					}
				}
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (TimeoutException e) {
				e.printStackTrace();
			} catch (AdbCommandRejectedException e) {
				e.printStackTrace();
			}
			
		}
	}
}
